/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.eproject.logictics.dao.khoa;

import aptech.eproject.logictics.db.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.*;
import javax.annotation.*;
import javax.ejb.*;
import javax.persistence.*;
/**
 *
 * @author zZzShaRazZz
 */
@Stateless
@TransactionManagement(value = TransactionManagementType.CONTAINER)
public class ViewCustomerList implements ViewCustomerListRemote {

    @Resource
    private SessionContext context;
    @PersistenceContext(name = "LogicticsService")
    EntityManager em;

    public List getCustomerList() {
        Query query = em.createQuery("SELECT c FROM Customer c");
        return query.getResultList();
    }

    public boolean changeCustomerStatus(String username) {
        try {
           Customer customer = em.find(Customer.class, username);
           if (customer.getIsBlocked()) {
               customer.setIsBlocked(false);
               em.merge(customer);
           } else {
               customer.setIsBlocked(true);
               em.merge(customer);
           }
           return true;
        } catch (NullPointerException npe) {
            npe.printStackTrace();
            context.setRollbackOnly();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            context.setRollbackOnly();
            return false;
        }
    }

    public List getCustomerListByName(String name) {
        Query query = em.createNamedQuery("findCustomerByName");
        query.setParameter(1, "%" + name + "%");
        return query.getResultList();
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 
}
